package com.easychat.controller;

import com.easychat.annotation.GlobalInterceptor;
import com.easychat.entity.constants.Constants;
import com.easychat.entity.dto.TokenUserInfoDto;
import com.easychat.entity.enums.ResponseCodeEnum;
import com.easychat.entity.enums.UserContactStatusEnum;
import com.easychat.entity.enums.UserContactTypeEnum;
import com.easychat.entity.po.GroupInfo;
import com.easychat.entity.po.UserContact;
import com.easychat.entity.po.UserInfo;
import com.easychat.entity.vo.ResponseVO;
import com.easychat.entity.vo.account.response.LoginRspVO;
import com.easychat.exception.BusinessException;
import com.easychat.service.GroupInfoService;
import com.easychat.service.UserContactService;
import com.easychat.service.UserInfoService;
import com.easychat.utils.CopyTools;
import com.easychat.utils.StringTools;
import com.easychat.websocket.ChannelContextUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

/**
 * @Author: yudaxian
 * @Description: TODO
 * @DateTime: 2024/8/15 0:00
 **/
@RestController
@RequestMapping("/userInfo")
public class UserInfoController extends ABaseController {

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private GroupInfoService groupInfoService;

    @Resource
    private UserContactService userContactService;

    @Resource
    private ChannelContextUtils channelContextUtils;

    /**
     * 获取当前用户信息
     * @param request
     * @return
     */
    @GlobalInterceptor
    @RequestMapping("/getUserInfo")
    public ResponseVO getUserInfo(HttpServletRequest request) {
        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
        UserInfo userInfo = userInfoService.getUserInfoByUserId(tokenUserInfo.getUserId());
        LoginRspVO vo = CopyTools.copy(userInfo, LoginRspVO.class);
        vo.setAdmin(tokenUserInfo.getAdmin());
        vo.setToken(tokenUserInfo.getToken());
        vo.setContactStatus(UserContactTypeEnum.getByPrefix(tokenUserInfo.getUserId()).getType());
        return getSuccessResponseVO(vo);
    }

    /**
     * 保存用户信息
     * @param request
     * @return
     */
    @GlobalInterceptor
    @RequestMapping("/saveUserInfo")
    public ResponseVO saveUserInfo(HttpServletRequest request, UserInfo userInfo,
                                   MultipartFile avatarFile, MultipartFile avatarCover) {
        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
        userInfo.setUserId(tokenUserInfo.getUserId());
        userInfo.setStatus(null);
        userInfo.setCreateTime(null);
        userInfo.setLastLoginTime(null);
        userInfo.setPassword(null);

        userInfoService.updateUserInfo(userInfo, avatarFile, avatarCover);

        return getSuccessResponseVO(request);
    }

    @GlobalInterceptor
    @RequestMapping("/updatePassword")
    public ResponseVO updatePassword(HttpServletRequest request, @NotEmpty @Pattern(regexp = Constants.PASSWORD_REGEX) String password) {
        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);

        UserInfo userInfo = new UserInfo();
        userInfo.setPassword(StringTools.encodeMD5(password));
        userInfoService.updateUserInfoByUserId(userInfo, tokenUserInfo.getUserId());
        channelContextUtils.closeContext(tokenUserInfo.getUserId());
        return getSuccessResponseVO(null);
    }

    @GlobalInterceptor
    @RequestMapping("/logout")
    public ResponseVO logout(HttpServletRequest request) {
        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
        channelContextUtils.closeContext(tokenUserInfo.getUserId());
        return getSuccessResponseVO(null);
    }

}
